/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2010 University College London 
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Author: Joao Taveira Araujo <j.araujo@ucl.ac.uk>
 */

#ifndef IPV4_REACT_TABLE_ENTRY_H
#define IPV4_REACT_TABLE_ENTRY_H

#include <list>
#include <vector>
#include <ostream>
 
#include "ns3/object.h"
#include "ns3/callback.h"
#include "ns3/traced-value.h" 
#include "ns3/ipv4-address.h"
#include "ns3/simulator.h"

namespace ns3 {

/**
 * \ingroup routing
 *
 * A record of an IPv4 react routing table entry for Ipv4ReactRouting
 */
class Ipv4ReactTableEntry : public Object {
public:
  static TypeId GetTypeId (void);

  enum {
    NON_ECT,
    ECT,
    POS,
    CE,
    FNE,
    RECT,
    LE,
    CANC,
    MAX_CODEPOINT
  };

  /**
   * \brief This constructor does nothing
   */
  Ipv4ReactTableEntry ();
  /**
   * \return An Ipv4ReactTableEntry object corresponding to the input parameters.
   * \param network Ipv4Address of the destination network
   * \param networkMask Ipv4Mask of the destination network mask
   * \param interface Outgoing interface 
   */
  Ipv4ReactTableEntry (Ipv4Address dest, Ipv4Mask mask, uint32_t interface);
  /**
   * \return The IPv4 network number of the destination of this route
   */
  Ipv4Address GetDestNetwork (void) const;
  /**
   * \return The IPv4 network mask of the destination of this route
   */
  Ipv4Mask GetDestNetworkMask (void) const;
  /**
   * \return The Ipv4 interface number used for sending outgoing packets
   */
  uint32_t GetInterface (void) const;



  /*=== PREF ==*/
  
  /**
   * \return Value of counter updated by respective flow window 
   */
  double IncrementTokenCount();
  /**
   * \brief Signals route was selected, which reduces token counter by 1 
   */
  void   DecrementTokenCount();
  /**
    * \param Set flow split 
    */
  void SetFlowSplit (double);
  /**
    * \return Current ratio of flows for destination which passes through this route
    */
  double GetFlowSplit () const;
  /**
    * \return Returns txmit^2/rexmit. Used for Pref calculations 
    */
  double GetInstTheta ();  
  /**
    * \return Returns txmit^2/rexmit. Used for Pref calculations 
    */
  double GetWghtTheta ();  


  /*=== LEX ==*/
  
  /**
   * \brief Updates route reflex counter with sent packet
   * \param codepoint Value of LEX codepoint in packet
   * \param value Size of packet
   */
  void CountLex (int codepoint, const uint32_t value);
  /**
    * \brief Updates variables with counter values, resets temp counters for next timestep 
    */
  void Timestep ();
  /**
    * \brief Clears temporary byte counters
    */
  void Clear ();
  /**
    * \return Returns total bytes received
    */
  double GetInstTxmit ();  
  /**
    * \return Returns total bytes received
    */
  double GetWghtTxmit ();  
  /**
    * \return Returns total rxbytes received
    */
  double GetWghtRexmit ();  
  /**
    * \return Returns total rxbytes received
    */
  double GetInstRexmit ();  
  /**
    * \return Get weighted average of Loss in last timestep
    */
  double GetWghtLoss () const;
  /**
    * \return Get instantaneous value of Loss in last timestep
    */
  double GetInstLoss () const;

private:
  Ipv4Address m_dest;
  Ipv4Mask m_destNetworkMask;
  uint32_t m_interface;

  //reflex counters
  uint32_t m_count[MAX_CODEPOINT];

  //weighted values of Txmit and Rexmit (bytes)
  double m_wghtTxmit;
  double m_wghtRexmit;

  //instantaneous values of Txmit and Rexmit (bytes)
  double m_tmpTxmit;
  double m_instTxmit;
  double m_instRexmit;

  double m_instTheta;
  double m_wghtTheta;

  double   m_tokenCounter;      //"token counter for load balancing"

  //attributes
  double m_alpha;

  //traced
  TracedValue<double> m_flowSplit;   
};

std::ostream& operator<< (std::ostream& os, Ipv4ReactTableEntry const& route);

}//namespace ns3

#endif /* IPV4_REACT_TABLE_ENTRY_H */
